package com.open.rbac.service.sys.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.sys.RoleMenuRel;


public interface RoleMenuRelRepository extends JpaRepository<RoleMenuRel, String> {

    /**
     * 按角色Id物理删除'角色与菜单关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM role_menu_rel rel WHERE rel.roleId = ?1")
    void deleteReallyByRoleId(String roleId);

    /**
     * 按菜单Id物理删除'角色与菜单关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM role_menu_rel rel WHERE rel.menuId = ?1")
    void deleteReallyByMenuId(String menuId);

    @Query("""
        SELECT DISTINCT t1 FROM role_menu_rel t1
        INNER JOIN role_user_rel t2
        ON t1.roleId = t2.roleId
        WHERE t2.userId = ?1
        """)
    List<RoleMenuRel> findByUserId(String userId);

}
